package com.qiyun.dinghuohui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.qiyun.dinghuohui.R;
import com.qiyun.dinghuohui.base.BaseActivity;
import com.qiyun.dinghuohui.common.ClientCookie;
import com.qiyun.dinghuohui.common.CommonDataSets;
import com.qiyun.dinghuohui.common.GlideImageLoader;
import com.qiyun.dinghuohui.common.ImageUtils;
import com.qiyun.dinghuohui.utils.CommonUtil;
import com.qiyun.dinghuohui.utils.Inject;
import com.qiyun.dinghuohui.utils.ServerResponse;
import com.qiyun.dinghuohui.vo.PublicSelectParamBean;
import com.qiyun.dinghuohui.widget.DividerItemDecoration;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xlm on 2018\6\8 0008
 */

public class DisplaysActivity extends BaseActivity {

    @Inject(R.id.back)
    private RelativeLayout mRelaBack;//返回
    @Inject(R.id.title)
    private TextView mTxtTitle;//标题

    @Inject(R.id.banner)
    private Banner banner;//轮播图

    private JSONObject object;//陈列

    private JSONObject goodObject;//单品详情

    private String title;//

    @Inject(R.id.display_recycle)
    private RecyclerView mRecycleDis;

    @Override
    protected void onGetIntent(Intent intent) {
        super.onGetIntent(intent);
        if (intent != null) {
            try {
                object = new JSONObject(intent.getStringExtra("data"));
                title = object.optString("display");
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    public static void startActivity(Activity context, JSONObject object) {
        Intent intent = new Intent(context, DisplaysActivity.class);
        intent.putExtra("data", object.toString());
        context.startActivity(intent);
    }

    //获取轮播图
    private List<String> getImagsPath() {
        List<String> imags = new ArrayList<>();
        if (object != null && object.length() != 0) {
            imags.add(ClientCookie.getServerUrl() + "/goods/display/" + object.optString("display") + ".jpg");
            JSONArray array = object.optJSONArray("disData");
            for (int i = 0; i < array.length(); i++) {
                imags.add(ClientCookie.getServerUrl() + "/goods/img/" + array.optJSONObject(i).optString("code") + ".jpg");
            }
        }
        return imags;
    }

    @Override
    protected void onSetContentView() {
        setContentView(R.layout.activity_displays);
    }

    @Override
    protected void initViews() {
        mRelaBack.setOnClickListener(getCurrentFragment());
        if (!CommonUtil.isEmpty(title)) {
            mTxtTitle.setText(String.valueOf(title));
        }
    }

    @Override
    protected void onSetListener() {
        mRelaBack.setOnClickListener(getThis());
        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                if (position == 0) {
                    BigPictureActivity.startActivity(getThis(), getImagsPath().get(position));
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;
        }
    }

    @Override
    protected void afterOnCreate(Bundle savedInstanceState) {
        banner.setImages(getImagsPath()).isAutoPlay(false).setImageLoader(new GlideImageLoader()).start();
        setDisplayAdapter();
    }

    //获取陈列的商品详情
    private List<JSONObject> getListDisplay() {
        List<JSONObject> list = new ArrayList<>();
        JSONArray array = object.optJSONArray("disData");
        for (int i = 0; i < array.length(); i++) {
            JSONObject object = array.optJSONObject(i);
            list.add(object);
        }
        return list;
    }


    private DisplayAdapter displayAdapter;

    private void setDisplayAdapter() {
        mRecycleDis.setLayoutManager(new GridLayoutManager(getThis(), 3, LinearLayoutManager.VERTICAL, false));
        mRecycleDis.addItemDecoration(new DividerItemDecoration(getThis(), DividerItemDecoration.GRID_LIST,
                getResources().getDrawable(R.drawable.shape_grid_div)));
        final List<JSONObject> list = getListDisplay();
        displayAdapter = new DisplayAdapter(R.layout.item_displays, list);
        mRecycleDis.setAdapter(displayAdapter);
        displayAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                DisplaysDetailActivity.startActivity(getThis(), CommonDataSets.getGoodsByCode(list.get(position).optString("code")));
            }
        });
    }

    private class DisplayAdapter extends BaseQuickAdapter<JSONObject, BaseViewHolder> {


        public DisplayAdapter(int layoutResId, @Nullable List<JSONObject> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, JSONObject item) {
            int width = CommonUtil.getDisplayMetrics(getThis()).widthPixels / 3;
//                  helper.itemView.getLayoutParams().height = width;
            helper.itemView.getLayoutParams().width = width - 1;
//            ImageUtils.showImage(getThis(), (ImageView) helper.getView(R.id.item_display_image),
//                    ClientCookie.getServerUrl() + "/goods/img/" + "1504" + ".jpg");
            ImageUtils.showImage(getThis(), (ImageView) helper.getView(R.id.item_display_image),
                    ImageUtils.imagUrl);
            helper.setText(R.id.item_display_code, item.optString("code"));
        }
    }

    @Override
    public void onPostDataStart() {

    }

    @Override
    public void onPostDataSuccess(ServerResponse response) {

    }

    @Override
    public void onPostDataError(ServerResponse response) {

    }

    @Override
    public void onPostDataComplete(ServerResponse response) {

    }

    @Override
    public void setProgressMessage(String message) {

    }

    @Override
    public void publicSelectCallBack(PublicSelectParamBean pspb) {

    }


}
